home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / uucosr10.arc / ENCODE.C < prev    next >
C/C++ Source or Header  |  1991-05-31  |  3KB  |  182 lines

  1. /*
  2.  * This was modified heavily for GEM from DUMAS UUE.C
  3.  */ 
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7.  
  8. /* ENC is the basic 1 character encoding function to make a char printing */
  9.  
  10. #define ENC(c) (((c) & 077) + ' ')
  11.  
  12. extern FILE  *fopen();
  13. FILE *fp, *outp;
  14. char ofname[80];
  15. int lenofname;
  16. int stdo = 0;
  17.  
  18. #define READ "rb"
  19.  
  20. int part = 'a', chap = 'a';
  21. #define SEQMAX 'z'
  22. #define SEQMIN 'a'
  23. char seqc = SEQMAX;
  24.  
  25. int split = 0; fileln = 32*1024;
  26. char lnbuf[128];
  27. encode(filename)
  28.     char *filename;
  29.     {
  30.     char *p;
  31.     char *fname;
  32.     if ((fp=fopen(filename, READ))==NULL) 
  33.         {
  34.         sprintf(lnbuf,"[1][Unable to open|%s][ABORT]",filename);
  35.         form_alert(1,lnbuf);
  36.         return;
  37.         }
  38.     disp_process(filename);
  39.     p = filename+strlen(filename);
  40.     while (--p > filename)
  41.         {
  42.         if ((*(p-1) == '\\')||(*(p-1) == ':'))
  43.             break;
  44.         }
  45.     /* p now points to the root filename, no path */
  46.     strcpy(ofname, p);
  47.     fname = ofname;
  48.     do 
  49.         {
  50.         if (*fname == '.')
  51.             *fname = '\0';
  52.         } while (*fname++);
  53.     /* 8 char prefix + .uue -> 12 chars MAX */
  54.     lenofname = strlen(ofname);
  55.     if (lenofname > 8) ofname[8] = '\0';
  56.     strcat(ofname,".uue");
  57.     lenofname = strlen(ofname);
  58.     makename();
  59.     if((outp = fopen(ofname, "w")) == NULL) 
  60.         {
  61.         sprintf(lnbuf,"[1][Unable to open|%s][ABORT]",ofname);
  62.         form_alert(1,lnbuf);
  63.         fclose(fp);
  64.         return;
  65.         }
  66.     maketable();
  67.     fprintf(outp,"begin %o %s\n", 0644, p);
  68.     uuencode();
  69.     fprintf(outp,"end\n");
  70.     fclose(outp);
  71.     return;
  72.     }
  73.  
  74. /* create ASCII table so a mailer can screw it up and the decode
  75.  * program can restore the error.
  76.  */
  77. maketable()
  78. {
  79.     register int i, j;
  80.  
  81.     fputs("table\n", outp);
  82.     for(i = ' ', j = 0; i < '`' ; j++) {
  83.         if (j == 32)
  84.             putc('\n', outp);
  85.         fputc(i++, outp);
  86.     }
  87.     putc('\n', outp);
  88. }
  89.  
  90. /*
  91.  * Generate the names needed for single and multiple part encoding.
  92.  */
  93. makename()
  94. {
  95.     if (split) {
  96.         ofname[lenofname - 1] = part;
  97.         ofname[lenofname - 2] = chap;
  98.     }
  99. }
  100.  
  101. /*
  102.  * copy from in to out, encoding as you go along.
  103.  */
  104. uuencode()
  105. {
  106.     char buf[80];
  107.     register int i, n;
  108.     register int lines;
  109.     lines = 6;
  110.  
  111.     for (;;) {
  112.         n = fr(buf, 45);
  113.         putc(ENC(n), outp);
  114.         for (i = 0; i < n; i += 3)
  115.               outdec(&buf[i]);
  116.         putc(seqc, outp);
  117.         seqc--;
  118.         if (seqc < SEQMIN) seqc = SEQMAX;
  119.         putc('\n', outp);
  120.         ++lines;
  121.         if (split && (lines > fileln)) {
  122.             part++;
  123.             if (part > 'z') {
  124.                 part = 'a';
  125.                 if (chap == 'z')
  126.                     chap = 'a'; /* loop ... */
  127.                 else
  128.                     chap++;
  129.             }
  130.             makename();
  131.             fprintf(outp,"include %s\n",ofname);
  132.             fclose(outp);
  133.             if((outp = fopen(ofname, "w")) == NULL) 
  134.                 {
  135.                 sprintf(lnbuf,"[1][Unable to open|%s][ABORT]",
  136.                         ofname);
  137.                 form_alert(1,lnbuf);
  138.                 return;
  139.                 }
  140.             maketable();
  141.             fprintf(outp,"begin part %c %s\n",part,ofname);
  142.             lines = 6;
  143.         }
  144.         if (n <= 0)
  145.             break;
  146.     }
  147. }
  148.  
  149. /*
  150.  * output one group of 3 bytes, pointed at by p, on file f.
  151.  */
  152. outdec(p)
  153. register char *p;
  154. {
  155.     register int c1, c2, c3, c4;
  156.  
  157.     c1 = *p >> 2;
  158.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  159.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  160.     c4 = p[2] & 077;
  161.     putc(ENC(c1), outp);
  162.     putc(ENC(c2), outp);
  163.     putc(ENC(c3), outp);
  164.     putc(ENC(c4), outp);
  165. }
  166.  
  167. /* fr: like read but stdio */
  168. int fr(buf, cnt)
  169. register char *buf;
  170. register int cnt;
  171. {
  172.     register int c, i;
  173.     for (i = 0; i < cnt; i++) {
  174.         c = fgetc(fp);
  175.         if (feof(fp))
  176.             return(i);
  177.         buf[i] = c;
  178.     }
  179.     return (cnt);
  180. }
  181.  
  182.